#include "EditConfigDlg.h"
#include "EditWindow.h"
#include <stdio.h>
#include <assert.h>
#include <shlwapi.h>

CEditConfigDlg::CEditConfigDlg()
{
    GetModuleFileName(_Module.GetModuleInstance(), m_syntaxFilename, _MAX_PATH);
    PathRemoveFileSpec(m_syntaxFilename);
    strcat(m_syntaxFilename, "\\");
    strcat(m_syntaxFilename, DEFAULT_SYNTAXFILE);

    GetModuleFileName(_Module.GetModuleInstance(), m_configFilename, _MAX_PATH);
    PathRemoveFileSpec(m_configFilename);
    strcat(m_configFilename, "\\");
    strcat(m_configFilename, CONFIG_FILE);
    m_bSpaceForTab = false;
    m_uTabSize = 3;
}

CEditConfigDlg::~CEditConfigDlg()
{
}

#define MAX_DIGITS 32

LRESULT CEditConfigDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    LoadConfigurationData();

    m_hSpaceForTab = GetDlgItem(IDC_SPACEFORTAB);
    DWORD checked = (m_bSpaceForTab ? BST_CHECKED : BST_UNCHECKED);
    ::SendMessage(m_hSpaceForTab, BM_SETCHECK, checked, 0);

    m_hTabSize = GetDlgItem(IDC_TABSIZE);
    char size[MAX_DIGITS];
    _snprintf(size, MAX_DIGITS, "%d", m_uTabSize);
    ::SetWindowText(m_hTabSize, size);

    m_hSyntaxFile = GetDlgItem(IDC_FILENAME);
    ::SetWindowText(m_hSyntaxFile, m_syntaxFilename);
    ::SetFocus(m_hSpaceForTab);
    CenterWindow (GetParent());
    ShowWindow(SW_SHOW);
    bHandled = TRUE;
    return TRUE;
}

LRESULT CEditConfigDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    return 0;
}

LRESULT CEditConfigDlg::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    LRESULT checked = ::SendMessage(m_hSpaceForTab, BM_GETCHECK, 0, 0);
    switch (checked) {
    case BST_CHECKED:
        m_bSpaceForTab = true;
        break;
    case BST_UNCHECKED:
        m_bSpaceForTab = false;
        break;
    default:
        assert (0);
    }

    char tabsize[MAX_DIGITS];
    LRESULT count = ::GetWindowText(m_hTabSize, tabsize, MAX_DIGITS);
    sscanf (tabsize, "%d", &m_uTabSize);

    count = ::GetWindowText(m_hSyntaxFile, m_syntaxFilename, _MAX_PATH);

    StoreConfigurationData();
    EndDialog(IDOK);
    return 0;
}

LRESULT CEditConfigDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    EndDialog(IDCANCEL);
    return 0;
}

LRESULT CEditConfigDlg::OnBrowse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    OPENFILENAME ofn;       // common dialog box structure
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = m_hWnd;
    ofn.lpstrFile = m_syntaxFilename;
    ofn.nMaxFile = _MAX_PATH;
    ofn.lpstrFilter = NULL;
    ofn.nFilterIndex = 0;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    if (GetOpenFileName(&ofn) != FALSE) {
        ::SetWindowText(m_hSyntaxFile, m_syntaxFilename);
    }
    return 0;
}

#define LINE_MAX 1024

void CEditConfigDlg::LoadConfigurationData()
{
    char line[LINE_MAX];

    FILE* f = fopen (m_configFilename, "r");
    if (!f) {
        return;
    }
    char* value;
    while (fgets(line, LINE_MAX, f)) {
        if (strncmp (line, SPACEFORTAB, strlen (SPACEFORTAB)) == 0) {
            // Todo;
            value = line + strlen (SPACEFORTAB);
            sscanf (value, "%d", &m_bSpaceForTab);
        } else if (strncmp (line, TABWIDTH, strlen (TABWIDTH)) == 0) {
            value = line + strlen (TABWIDTH);
            sscanf (value, "%d", &m_uTabSize);
            // Todo
        } else if (strncmp (line, SYNTAXFILE, strlen (SYNTAXFILE)) == 0) {
            // Todo
            value = line + strlen (SYNTAXFILE);
            strncpy(m_syntaxFilename, value, _MAX_PATH);
            unsigned int i = 0;
            while (i < strlen (m_syntaxFilename) && m_syntaxFilename[i] != '\r' && m_syntaxFilename[i] != '\n') {
                i++;
            }
            m_syntaxFilename[i] = '\0';
        }
    }
    fclose (f);
}

void CEditConfigDlg::StoreConfigurationData()
{
    char line[LINE_MAX];

    FILE* f = fopen (m_configFilename, "w");
    if (!f) {
        return;
    }

    _snprintf (line, LINE_MAX, "%s%d\n", SPACEFORTAB, m_bSpaceForTab);
    fputs(line, f);
    _snprintf (line, LINE_MAX, "%s%d\n", TABWIDTH, m_uTabSize);
    fputs(line, f);
    _snprintf (line, LINE_MAX, "%s%s\n", SYNTAXFILE, m_syntaxFilename);
    fputs(line, f);

    fclose (f);
}


